Skip to content

Fix sign handling of alias types for binary operators and casts - #410

Open
andidr wants to merge 1 commit into
llvm:mainfrom
andidr:main
Open

andidr wants to merge 1 commit into
llvm:mainfrom
andidr:main

Conversation

@andidr

@andidr andidr commented May 27, 2024

Copy link
Copy Markdown
Contributor

The generated IR for some of the AST nodes handled by MLIRScanner::VisitBinaryOperator() and
MLIRScanner::VisitCastExpr() depends on the sign of the operand or result types. Some of these checks attempt to directly cast the result of Expr::getType() to BuiltinType and then check if the resulting QualType is a signed or unsigned integer type. If the cast fails, the type is assumed to be signed.

However, the type being checked may not directly be a builtin type (e.g., it may be an alias for an unsigned type wrapped in a TypedefType) and the default assumption may not hold.

Examining the canonical type retrieved via
QualType::getCanonicalType(), as implemented by this change, solves the issue.

The generated IR for some of the AST nodes handled by
`MLIRScanner::VisitBinaryOperator()` and
`MLIRScanner::VisitCastExpr()` depends on the sign of the operand or
result types. Some of these checks attempt to directly cast the result
of `Expr::getType()` to `BuiltinType` and then check if the resulting
`QualType` is a signed or unsigned integer type. If the cast fails,
the type is assumed to be signed.

However, the type being checked may not directly be a builtin type
(e.g., it may be an alias for an unsigned type wrapped in a
`TypedefType`) and the default assumption may not hold.

Examining the canonical type retrieved via
`QualType::getCanonicalType()`, as implemented by this change, solves
the issue.
@andidr

andidr commented Jul 31, 2024 •

Copy link
Copy Markdown
Contributor Author

Kindly asking if anyone is in for a review of this PR. Considering the contributions to the affected files, maybe @wsmoses?

@Flinner

Flinner commented Nov 1, 2026

Copy link
Copy Markdown

Thanks for this. I hit the same bug independently and can confirm the fix. There are four more sites with the same pattern in VisitCastExpr that this PR doesn't cover yet.

Minimal reproducer (plain C):

typedef unsigned int u32;
long long widen(u32 x) { return x; }   // arith.extsi, should be extui
typedef unsigned char u8;
int div3(u8 x) { return x / 3; }        // arith.extsi i8 -> i32

Written directly as unsigned int / unsigned char, both functions correctly emit arith.extui. HLS-style C++ hits this everywhere, because every width is a typedef (bit8, ap_uint<8>, ...).

Remaining sites (same fix, getType().getCanonicalType()), line numbers against main:

@@ -4122 (CK_IntegralToFloating)
-    if (auto bit = dyn_cast<clang::BuiltinType>(&*E->getSubExpr()->getType())) {
+    if (auto bit = dyn_cast<clang::BuiltinType>(&*E->getSubExpr()->getType().getCanonicalType())) {
@@ -4141 (CK_FloatingToIntegral)
-    if (auto bit = dyn_cast<clang::BuiltinType>(&*E->getType())) {
+    if (auto bit = dyn_cast<clang::BuiltinType>(&*E->getType().getCanonicalType())) {
@@ -4366 (CK_IntegralToBoolean)
-    if (auto bit = dyn_cast<clang::BuiltinType>(&*E->getType())) {
+    if (auto bit = dyn_cast<clang::BuiltinType>(&*E->getType().getCanonicalType())) {
@@ -4386 (CK_FloatingToBoolean)
-    if (auto bit = dyn_cast<clang::BuiltinType>(&*E->getType())) {
+    if (auto bit = dyn_cast<clang::BuiltinType>(&*E->getType().getCanonicalType())) {

Verification: a differential test compared the same code compiled with clang and executed against the cgeist lowering run through mlir-runner. It covered widths 8/16/32/64, signed and unsigned, each written as the builtin type, a typedef and an alias template, across extension, /, %, >>, <, >, wrapping +/*, int↔double and conversion to bool.

  • main (2024-07-31): 768 mismatches, every one in an unsigned typedef or alias spelling.
  • All nine sites canonicalized: 7,656 of 7,656 values agree.

Happy to open a follow-up PR on top of this one if that's easier, or you're welcome to fold these four lines in.

Generated with Claude Opus 5.5

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants